home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / share / hplip / fab.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-10-28  |  20KB  |  662 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __version__ = '6.0'
  5. __title__ = 'Fax Address Book'
  6. __mod__ = 'hp-fab'
  7. __doc__ = 'A simple fax address book for HPLIP.'
  8. import cmd
  9. import getopt
  10. import os
  11. from base.g import *
  12. from base import utils, tui, module
  13.  
  14. class Console(cmd.Cmd):
  15.     
  16.     def __init__(self):
  17.         cmd.Cmd.__init__(self)
  18.         self.intro = "Type 'help' for a list of commands. Type 'exit' or 'quit' to quit."
  19.         self.db = fax.FaxAddressBook()
  20.         self.prompt = log.bold('hp-fab > ')
  21.  
  22.     
  23.     def do_hist(self, args):
  24.         '''Print a list of commands that have been entered'''
  25.         print self._hist
  26.  
  27.     
  28.     def do_exit(self, args):
  29.         '''Exits from the console'''
  30.         return -1
  31.  
  32.     
  33.     def do_quit(self, args):
  34.         '''Exits from the console'''
  35.         return -1
  36.  
  37.     
  38.     def do_EOF(self, args):
  39.         '''Exit on system end of file character'''
  40.         return self.do_exit(args)
  41.  
  42.     
  43.     def do_help(self, args):
  44.         """Get help on commands
  45.            'help' or '?' with no arguments prints a list of commands for which help is available
  46.            'help <command>' or '? <command>' gives help on <command>
  47.         """
  48.         cmd.Cmd.do_help(self, args)
  49.  
  50.     
  51.     def preloop(self):
  52.         '''Initialization before prompting user for commands.
  53.            Despite the claims in the Cmd documentaion, Cmd.preloop() is not a stub.
  54.         '''
  55.         cmd.Cmd.preloop(self)
  56.         self._hist = []
  57.         self._locals = { }
  58.         self._globals = { }
  59.         self.do_list('')
  60.  
  61.     
  62.     def postloop(self):
  63.         '''Take care of any unfinished business.
  64.            Despite the claims in the Cmd documentaion, Cmd.postloop() is not a stub.
  65.         '''
  66.         cmd.Cmd.postloop(self)
  67.         print 'Exiting...'
  68.  
  69.     
  70.     def precmd(self, line):
  71.         ''' This method is called after the line has been input but before
  72.             it has been interpreted. If you want to modifdy the input line
  73.             before execution (for example, variable substitution) do it here.
  74.         '''
  75.         self._hist += [
  76.             line.strip()]
  77.         return line
  78.  
  79.     
  80.     def postcmd(self, stop, line):
  81.         '''If you want to stop the console, return something that evaluates to true.
  82.            If you want to do some post command processing, do it here.
  83.         '''
  84.         return stop
  85.  
  86.     
  87.     def emptyline(self):
  88.         '''Do nothing on empty input line'''
  89.         pass
  90.  
  91.     
  92.     def default(self, line):
  93.         log.error("Unrecognized command. Use 'help' to list commands.")
  94.  
  95.     
  96.     def get_nickname(self, args, fail_if_match = True, alt_text = False):
  97.         if not args:
  98.             while True:
  99.                 if alt_text:
  100.                     nickname = raw_input(log.bold('Enter the name to add to the group (<enter>=done*, c=cancel) ? ')).strip()
  101.                 else:
  102.                     nickname = raw_input(log.bold('Enter name (c=cancel) ? ')).strip()
  103.                 if nickname.lower() == 'c':
  104.                     print log.red('Canceled')
  105.                     return ''
  106.                 if fail_if_match:
  107.                     if self.db.get(nickname) is not None:
  108.                         log.error('Name already exists. Please choose a different name.')
  109.                         continue
  110.                     
  111.                 elif self.db.get(nickname) is None:
  112.                     log.error('Name not found. Please enter a different name.')
  113.                     continue
  114.                 
  115.                 break
  116.                 continue
  117.                 None if not nickname else nickname.lower() == 'c'
  118.         else:
  119.             nickname = args.strip()
  120.             if fail_if_match:
  121.                 if self.db.get(nickname) is not None:
  122.                     log.error('Name already exists. Please choose a different name.')
  123.                     return ''
  124.             elif self.db.get(nickname) is None:
  125.                 log.error('Name not found. Please enter a different name.')
  126.                 return ''
  127.         return nickname
  128.  
  129.     
  130.     def get_groupname(self, args, fail_if_match = True, alt_text = False):
  131.         all_groups = self.db.get_all_groups()
  132.         if not args:
  133.             while True:
  134.                 if alt_text:
  135.                     groupname = raw_input(log.bold('Enter the group to join (<enter>=done*, c=cancel) ? ')).strip()
  136.                 else:
  137.                     groupname = raw_input(log.bold('Enter the group (c=cancel) ? ')).strip()
  138.                 if groupname.lower() == 'c':
  139.                     print log.red('Canceled')
  140.                     return ''
  141.                 if groupname == 'All':
  142.                     print "Cannot specify group 'All'. Please choose a different group."
  143.                     return ''
  144.                 if fail_if_match:
  145.                     if groupname in all_groups:
  146.                         log.error('Group already exists. Please choose a different group.')
  147.                         continue
  148.                     
  149.                 elif groupname not in all_groups:
  150.                     log.error('Group not found. Please enter a different group.')
  151.                     continue
  152.                 
  153.                 break
  154.         else:
  155.             groupname = args.strip()
  156.             if fail_if_match:
  157.                 if groupname in all_groups:
  158.                     log.error('Group already exists. Please choose a different group.')
  159.                     return ''
  160.             elif groupname not in all_groups:
  161.                 log.error('Group not found. Please enter a different group.')
  162.                 return ''
  163.         return groupname
  164.  
  165.     
  166.     def do_list(self, args):
  167.         '''
  168.         List names and/or groups.
  169.         list [names|groups|all|]
  170.         dir [names|groups|all|]
  171.         '''
  172.         self.do_names('')
  173.         self.do_groups('')
  174.  
  175.     do_dir = do_list
  176.     
  177.     def do_names(self, args):
  178.         '''
  179.         List names.
  180.         names
  181.         '''
  182.         all_entries = self.db.get_all_records()
  183.         log.debug(all_entries)
  184.         print log.bold('\nNames:\n')
  185.         if len(all_entries) > 0:
  186.             f = tui.Formatter()
  187.             f.header = ('Name', 'Fax Number', 'Notes', 'Member of Group(s)')
  188.             for name, e in all_entries.items():
  189.                 if not name.startswith('__'):
  190.                     f.add((name, e['fax'], e['notes'], ', '.join(e['groups'])))
  191.                     continue
  192.             
  193.             f.output()
  194.         else:
  195.             print '(None)'
  196.         print 
  197.  
  198.     
  199.     def do_groups(self, args):
  200.         '''
  201.         List groups.
  202.         groups
  203.         '''
  204.         all_groups = self.db.get_all_groups()
  205.         log.debug(all_groups)
  206.         print log.bold('\nGroups:\n')
  207.         if len(all_groups):
  208.             f = tui.Formatter()
  209.             f.header = ('Group', 'Members')
  210.             for group in all_groups:
  211.                 ', '.join(([], [](_[1])))
  212.             
  213.             f.output()
  214.         else:
  215.             print '(None)'
  216.         print 
  217.  
  218.     
  219.     def do_edit(self, args):
  220.         '''
  221.         Edit an name.
  222.         edit [name]
  223.         modify [name]
  224.         '''
  225.         nickname = self.get_nickname(args, fail_if_match = False)
  226.         if not nickname:
  227.             return None
  228.         e = self.db.get(nickname)
  229.         log.debug(e)
  230.         print log.bold('\nEdit/modify information for %s:\n' % nickname)
  231.         lastname = ''
  232.         firstname = ''
  233.         title = ''
  234.         save_faxnum = e['fax']
  235.         while True:
  236.             faxnum = raw_input(log.bold("Fax Number (<enter>='%s', c=cancel) ? " % save_faxnum)).strip()
  237.             if faxnum.lower() == 'c':
  238.                 print log.red('Canceled')
  239.                 return None
  240.             if not faxnum and not save_faxnum:
  241.                 log.error('Fax number must not be empty.')
  242.                 continue
  243.             
  244.             if not faxnum:
  245.                 faxnum = save_faxnum
  246.             
  247.             ok = True
  248.             for c in faxnum:
  249.                 if c not in '0123456789-(+) *#':
  250.                     log.error("Invalid characters in fax number. Fax number may only contain '0123456789-(+) '")
  251.                     ok = False
  252.                     break
  253.                     continue
  254.             
  255.             if ok:
  256.                 break
  257.                 continue
  258.         save_notes = e['notes']
  259.         notes = raw_input(log.bold("Notes (<enter>='%s', c=cancel) ? " % save_notes)).strip()
  260.         if notes.lower() == 'c':
  261.             print log.red('Canceled')
  262.             return None
  263.         if not notes:
  264.             notes = save_notes
  265.         
  266.         if e['groups']:
  267.             print '\nLeave or Stay in a Group:\n'
  268.         
  269.         new_groups = []
  270.         for g in e['groups']:
  271.             if g == 'All':
  272.                 continue
  273.             
  274.             (ok, ans) = tui.enter_yes_no('Stay in group %s ' % g, choice_prompt = '(y=yes* (stay), n=no (leave), c=cancel) ? ')
  275.             None if not ok else ok
  276.         
  277.         print '\nJoin New Group(s):\n'
  278.         while True:
  279.             add_group = self.get_groupname('', fail_if_match = False, alt_text = True)
  280.             if add_group.lower() == 'c':
  281.                 print log.red('Canceled')
  282.                 return None
  283.             if not add_group:
  284.                 break
  285.             
  286.             all_groups = self.db.get_all_groups()
  287.             if add_group not in all_groups:
  288.                 log.warn('Group not found.')
  289.                 (ok, ans) = tui.enter_yes_no('Is this a new group', choice_prompt = '(y=yes* (new), n=no, c=cancel) ? ')
  290.                 if not ok:
  291.                     print log.red('Canceled')
  292.                     return None
  293.                 if not ans:
  294.                     continue
  295.                 
  296.             
  297.             if add_group in e['groups']:
  298.                 log.error('Group already specified. Choose a different group name or press <enter> to continue.')
  299.                 continue
  300.             
  301.             new_groups.append(add_group)
  302.         self.db.set(nickname, title, firstname, lastname, faxnum, new_groups, notes)
  303.         self.do_show(nickname)
  304.         print 
  305.  
  306.     do_modify = do_edit
  307.     
  308.     def do_editgrp(self, args):
  309.         '''
  310.         Edit a group.
  311.         editgrp [group]
  312.         modifygrp [group]
  313.         '''
  314.         group = self.get_groupname(args, fail_if_match = False)
  315.         if not group:
  316.             return None
  317.         old_entries = self.db.group_members(group)
  318.         new_entries = []
  319.         print '\nExisting Names in Group:\n'
  320.         for e in old_entries:
  321.             if not e.startswith('__'):
  322.                 (ok, ans) = tui.enter_yes_no("Should '%s' stay in this group " % e, choice_prompt = '(y=yes* (stay), n=no (leave), c=cancel) ? ')
  323.             
  324.             e.startswith('__') if not ok else ok
  325.         
  326.         print '\nAdd New Names to Group:\n'
  327.         while True:
  328.             nickname = self.get_nickname('', fail_if_match = False, alt_text = True)
  329.             if nickname.lower() == 'c':
  330.                 print log.red('Canceled')
  331.                 return None
  332.             if not nickname.lower():
  333.                 break
  334.             
  335.             new_entries.append(nickname)
  336.         self.db.update_groups(group, new_entries)
  337.         print 
  338.  
  339.     do_modifygrp = do_editgrp
  340.     
  341.     def do_add(self, args):
  342.         '''
  343.         Add an name.
  344.         add [name]
  345.         new [name]
  346.         '''
  347.         nickname = self.get_nickname(args, fail_if_match = True)
  348.         if not nickname:
  349.             return None
  350.         print log.bold('\nEnter information for %s:\n' % nickname)
  351.         title = ''
  352.         firstname = ''
  353.         lastname = ''
  354.         while True:
  355.             faxnum = raw_input(log.bold('Fax Number (c=cancel) ? ')).strip()
  356.             if faxnum.lower() == 'c':
  357.                 print log.red('Canceled')
  358.                 return None
  359.             if not faxnum:
  360.                 log.error('Fax number must not be empty.')
  361.                 continue
  362.             
  363.             ok = True
  364.             for c in faxnum:
  365.                 if c not in '0123456789-(+) *#':
  366.                     log.error("Invalid characters in fax number. Fax number may only contain '0123456789-(+) *#'")
  367.                     ok = False
  368.                     break
  369.                     continue
  370.             
  371.             if ok:
  372.                 break
  373.                 continue
  374.         notes = raw_input(log.bold('Notes (c=cancel) ? ')).strip()
  375.         if notes.strip().lower() == 'c':
  376.             print log.red('Canceled')
  377.             return None
  378.         groups = []
  379.         all_groups = self.db.get_all_groups()
  380.         while True:
  381.             add_group = raw_input(log.bold('Member of group (<enter>=done*, c=cancel) ? ')).strip()
  382.             if add_group.lower() == 'c':
  383.                 print log.red('Canceled')
  384.                 return None
  385.             if add_group not in all_groups:
  386.                 log.warn('Group not found.')
  387.                 while True:
  388.                     user_input = raw_input(log.bold('Is this a new group (y=yes*, n=no) ? ')).lower().strip()
  389.                     if user_input not in ('', 'n', 'y'):
  390.                         log.error("Please enter 'y', 'n' or press <enter> for 'yes'.")
  391.                         continue
  392.                     
  393.                     break
  394.                 if user_input == 'n':
  395.                     continue
  396.                 
  397.             
  398.             if add_group in groups:
  399.                 log.error('Group already specified. Choose a different group name or press <enter> to continue.')
  400.                 continue
  401.             
  402.             groups.append(add_group)
  403.         groups.append('All')
  404.         self.db.set(nickname, title, firstname, lastname, faxnum, groups, notes)
  405.         self.do_show(nickname)
  406.  
  407.     do_new = do_add
  408.     
  409.     def do_addgrp(self, args):
  410.         '''
  411.         Add a group.
  412.         addgrp [group]
  413.         newgrp [group]
  414.         '''
  415.         group = self.get_groupname(args, fail_if_match = True)
  416.         if not group:
  417.             return None
  418.         entries = []
  419.         while True:
  420.             nickname = self.get_nickname('', fail_if_match = False, alt_text = True)
  421.             if nickname.lower() == 'c':
  422.                 print log.red('Canceled')
  423.                 return None
  424.             entries.append(nickname)
  425.             continue
  426.             None if not nickname.lower() else group
  427.         self.db.update_groups(group, entries)
  428.         print 
  429.  
  430.     do_newgrp = do_addgrp
  431.     
  432.     def do_view(self, args):
  433.         '''
  434.         View all name data.
  435.         view
  436.         '''
  437.         all_entries = self.db.get_all_records()
  438.         log.debug(all_entries)
  439.         print log.bold('\nView all Data:\n')
  440.         if len(all_entries) > 0:
  441.             f = tui.Formatter()
  442.             f.header = ('Name', 'Fax', 'Notes', 'Member of Group(s)')
  443.             for name, e in all_entries.items():
  444.                 if not name.startswith('__'):
  445.                     f.add((name, e['fax'], e['notes'], ', '.join(e['groups'])))
  446.                     continue
  447.             
  448.             f.output()
  449.         
  450.         print 
  451.  
  452.     
  453.     def do_show(self, args):
  454.         '''
  455.         Show a name (all details).
  456.         show [name]
  457.         details [name]
  458.         '''
  459.         name = self.get_nickname(args, fail_if_match = False)
  460.         if not name:
  461.             return None
  462.         e = self.db.get(name)
  463.         if e:
  464.             f = tui.Formatter()
  465.             f.header = ('Key', 'Value')
  466.             f.add(('Name:', name))
  467.             f.add(('Fax Number:', e['fax']))
  468.             f.add(('Notes:', e['notes']))
  469.             f.add(('Member of Group(s):', ', '.join(e['groups'])))
  470.             f.output()
  471.         else:
  472.             log.error("Name not found. Use the 'names' command to view all names.")
  473.         print 
  474.  
  475.     do_details = do_show
  476.     
  477.     def do_rm(self, args):
  478.         '''
  479.         Remove a name.
  480.         rm [name]
  481.         del [name]
  482.         '''
  483.         nickname = self.get_nickname(args, fail_if_match = False)
  484.         if not nickname:
  485.             return None
  486.         self.db.delete(nickname)
  487.         print 
  488.  
  489.     do_del = do_rm
  490.     
  491.     def do_rmgrp(self, args):
  492.         '''
  493.         Remove a group.
  494.         rmgrp [group]
  495.         delgrp [group]
  496.         '''
  497.         group = self.get_groupname(args, fail_if_match = False)
  498.         if not group:
  499.             return None
  500.         self.db.delete_group(group)
  501.         print 
  502.  
  503.     do_delgrp = do_rmgrp
  504.     
  505.     def do_about(self, args):
  506.         '''About fab.'''
  507.         utils.log_title(__title__, __version__)
  508.  
  509.     
  510.     def do_import(self, args):
  511.         '''
  512.         Import LDIF
  513.         import <filename> [type]
  514.         [type] = vcf|ldif|auto
  515.         '''
  516.         args = args.strip().split()
  517.         if not args:
  518.             log.error('You must specify a filename to import from.')
  519.             return None
  520.         filename = args[0]
  521.         if len(args) > 1:
  522.             typ = args[1].lower()
  523.         else:
  524.             typ = 'auto'
  525.         if typ not in ('auto', 'ldif', 'vcf', 'vcard'):
  526.             log.error('Invalid type: %s' % typ)
  527.             return None
  528.         if not os.path.exists(filename):
  529.             log.error('File %s not found.' % filename)
  530.             return None
  531.         if typ == 'ldif':
  532.             print 'Importing from LDIF file %s...' % filename
  533.             (ok, error_str) = self.db.import_ldif(filename)
  534.         elif typ in ('vcard', 'vcf'):
  535.             print 'Importing from VCF file %s...' % filename
  536.             (ok, error_str) = self.db.import_vcard(filename)
  537.         
  538.         if not ok:
  539.             log.error(error_str)
  540.         else:
  541.             self.do_list('')
  542.         print 
  543.  
  544.  
  545. mod = module.Module(__mod__, __title__, __version__, __doc__, None, (GUI_MODE, INTERACTIVE_MODE), (UI_TOOLKIT_QT3, UI_TOOLKIT_QT4))
  546. mod.setUsage(module.USAGE_FLAG_NONE)
  547. (opts, device_uri, printer_name, mode, ui_toolkit, loc) = mod.parseStdOpts(handle_device_printer = False)
  548. if ui_toolkit == 'qt3':
  549.     if not utils.canEnterGUIMode():
  550.         log.error('%s GUI mode requires GUI support (try running with --qt4). Entering interactive mode.' % __mod__)
  551.         mode = INTERACTIVE_MODE
  552.     
  553. elif not utils.canEnterGUIMode4():
  554.     log.error('%s GUI mode requires GUI support (try running with --qt3). Entering interactive mode.' % __mod__)
  555.     mode = INTERACTIVE_MODE
  556.  
  557. if mode == GUI_MODE:
  558.     if ui_toolkit == 'qt3':
  559.         log.set_module('hp-fab(qt3)')
  560.         
  561.         try:
  562.             from qt import *
  563.             from ui.faxaddrbookform import FaxAddrBookForm
  564.         except ImportError:
  565.             log.error('Unable to load Qt3 support. Is it installed?')
  566.             sys.exit(1)
  567.  
  568.         app = None
  569.         addrbook = None
  570.         app = QApplication(sys.argv)
  571.         if loc is None:
  572.             loc = user_conf.get('ui', 'loc', 'system')
  573.             if loc.lower() == 'system':
  574.                 loc = str(QTextCodec.locale())
  575.                 log.debug('Using system locale: %s' % loc)
  576.             
  577.         
  578.         if loc.lower() != 'c':
  579.             e = 'utf8'
  580.             
  581.             try:
  582.                 (l, x) = loc.split('.')
  583.                 loc = '.'.join([
  584.                     l,
  585.                     e])
  586.             except ValueError:
  587.                 l = loc
  588.                 loc = '.'.join([
  589.                     loc,
  590.                     e])
  591.  
  592.             log.debug('Trying to load .qm file for %s locale.' % loc)
  593.             trans = QTranslator(None)
  594.             qm_file = 'hplip_%s.qm' % l
  595.             log.debug('Name of .qm file: %s' % qm_file)
  596.             loaded = trans.load(qm_file, prop.localization_dir)
  597.             if loaded:
  598.                 app.installTranslator(trans)
  599.             else:
  600.                 loc = 'c'
  601.         
  602.         if loc == 'c':
  603.             log.debug("Using default 'C' locale")
  604.         else:
  605.             log.debug('Using locale: %s' % loc)
  606.             QLocale.setDefault(QLocale(loc))
  607.             prop.locale = loc
  608.             
  609.             try:
  610.                 locale.setlocale(locale.LC_ALL, locale.normalize(loc))
  611.             except locale.Error:
  612.                 pass
  613.  
  614.         addrbook = FaxAddrBookForm()
  615.         addrbook.show()
  616.         app.setMainWidget(addrbook)
  617.         
  618.         try:
  619.             log.debug('Starting GUI loop...')
  620.             app.exec_loop()
  621.         except KeyboardInterrupt:
  622.             pass
  623.  
  624.         sys.exit(0)
  625.     else:
  626.         
  627.         try:
  628.             from PyQt4.QtGui import QApplication
  629.             from ui4.fabwindow import FABWindow
  630.         except ImportError:
  631.             log.error('Unable to load Qt4 support. Is it installed?')
  632.             sys.exit(1)
  633.  
  634.         log.set_module('hp-fab(qt4)')
  635.         app = QApplication(sys.argv)
  636.         fab = FABWindow(None)
  637.         fab.show()
  638.         
  639.         try:
  640.             log.debug('Starting GUI loop...')
  641.             app.exec_()
  642.         except KeyboardInterrupt:
  643.             sys.exit(0)
  644.  
  645. else:
  646.     
  647.     try:
  648.         from fax import fax
  649.     except ImportError:
  650.         log.error('Fax address book disabled - Python 2.3+ required.')
  651.         sys.exit(1)
  652.  
  653.     console = Console()
  654.     
  655.     try:
  656.         console.cmdloop()
  657.     except KeyboardInterrupt:
  658.         log.error('User exit.')
  659.  
  660.     log.info('')
  661.     log.info('Done.')
  662.